package georah.map.graphics
{

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import mx.styles.ISimpleStyleClient;

	/**
	 *
	 * @author yuanm
	 */
	public final class StyledMarker extends MapGraphic
	{
		private var _skin:DisplayObject=null;

		override public function clone():MapGraphic
		{
			var m:StyledMarker=new StyledMarker();
			m.width=width;
			m.height=height;
			m.styleName=styleName;
			return m;
		}

		override public function styleChanged(styleProp:String):void
		{
			super.styleChanged(styleProp);

			if (_skin is ISimpleStyleClient)
			{
				(_skin as ISimpleStyleClient).styleName=this;
			}
			invalidateDisplayList();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			container.x=-0.5 * unscaledWidth;
			container.y=-0.5 * unscaledHeight;

			if (_skin)
			{
				if (container.contains(_skin))
				{
					container.removeChild(_skin);
				}
				_skin=null;
			}

			var skinStyle:*=getStyle("skin");
			if (skinStyle is DisplayObject)
			{
				_skin=skinStyle;
				if (_skin is Sprite)
				{
					(_skin as Sprite).mouseEnabled=false;
				}
			}

			if (skinStyle is Class)
			{
				_skin=new skinStyle();
			}

			if (_skin)
			{
				container.addChild(_skin);
				if (!width || !height)
				{
					width=_skin.width;
					height=_skin.height;
				}
				else
				{
					_skin.width=width;
					_skin.height=height;
				}
			}
			else
			{
				container.graphics.clear();
				container.graphics.beginFill(0xff0000, 0.6);
				container.graphics.drawRect(0, 0, width, height);
				container.graphics.endFill();
			}
		}

	}
}
